import os

def parse_model_config(path):
    """parses the yolo-v3 layer configuration file and return module definitions"""
    if not os.path.exists(path):
        print("file: {} not exits.")
        exit(-1)

    file = open(path, 'r', encoding="utf-8")
    lines = file.read().strip().split('\n')
    lines = [x for x in lines if x and not x.startswith('#')]#去掉注释
    lines = [x.rstrip().lstrip() for x in lines] # get rid of fringe whitespaces  去掉多余的空格

    module_def = []
    for line in lines:
        if line.startswith('['):# This marks the start of a new block  [ 代表的是一个块的开始
            module_def.append({})
            # 块的类别 如 net、convolutional、shortcut、yolo、route、upsample 等
            module_def[-1]['type'] = line[1:-1].rstrip()
            if module_def[-1]['type'] == 'convolutional':
                module_def[-1]['batch_normalize'] = 0
        else: # 每个块下面的内容如  batch_normalize、size 等参数，形成键值对
            key, value = line.split('=')
            key, value = key.strip(), value.strip()
            module_def[-1][key] = value

    return module_def # 返回是一个列表，列表中是每一个块的字典


